﻿using Apps.Common;
using Apps.Models;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Apps.DAL.HC
{
    public partial class HC_EXTEND_FIELD_Repository : DbContext<HC_EXTEND_FIELD>
    {
        /// <summary>
        /// 扩展列添加
        /// </summary>
        /// <param name="extend_field_model">扩展列实体</param>
        /// <param name="valueList">参考值集合</param>
        /// <returns></returns>
        public bool DoAdd(HC_EXTEND_FIELD extend_field_model, List<HC_FIELD_REFERENCE_VALUE> valueList)
        {
            try
            {
                Db.BeginTran();
                Db.Insertable(extend_field_model).ExecuteCommand();
                if (valueList != null)
                {
                    Db.Insertable(valueList).ExecuteCommand();
                }
                Db.CommitTran();
                return true;
            }
            catch (Exception ex)
            {
                Db.Ado.RollbackTran();
                return false;
            }
        }

        public List<HC_EXTEND_FIELD> GetList()
        {
            return Db.Queryable<HC_EXTEND_FIELD>().OrderBy("SORT DESC, ADDTIME DESC").Where(X => X.ENABLE == 1).ToList();
        }
        public List<HC_EXTEND_FIELD> GetList(string ids)
        {
            return Db.Queryable<HC_EXTEND_FIELD>().Where(it => ids.Contains(it.ID)).ToList();
        }
        public List<HC_EXTEND_FIELD> GetList(string queryStr, int pageIndex, int pageSize, string sortFiled, string orderBy, ref int totalCount)
        {
            var exp = Expressionable.Create<HC_EXTEND_FIELD>();
            exp.Or(it => it.NAME.Contains(queryStr));//拼接OR
            exp.Or(it => it.TITLE.Contains(queryStr));//拼接OR
            return Db.Queryable<HC_EXTEND_FIELD>().OrderBy(sortFiled + " " + orderBy).Where(exp.ToExpression()).ToPageList(pageIndex, pageSize, ref totalCount);
        }
        public List<HC_EXTEND_FIELD> GetList(int pageIndex, int pageSize, string sortFiled, string orderBy, ref int totalCount)
        {
            return Db.Queryable<HC_EXTEND_FIELD>().OrderBy(sortFiled + " " + orderBy).Where(x => x.ENABLE == 1).ToPageList(pageIndex, pageSize, ref totalCount);
        }

        /// <summary>
        /// 扩展列修改
        /// </summary>
        /// <param name="extend_field_model">扩展列实体</param>
        /// <param name="addvalueList">新增参考值集合</param>
        /// <param name="editvalueList">修改参考值集合</param>
        /// <returns></returns>
        public bool DoEdit(HC_EXTEND_FIELD extend_field_model, List<HC_FIELD_REFERENCE_VALUE> addvalueList, List<HC_FIELD_REFERENCE_VALUE> editvalueList)
        {
            try
            {
                Db.BeginTran();
                Db.Updateable<HC_EXTEND_FIELD>(extend_field_model).ExecuteCommand();
                if (addvalueList != null && addvalueList.Count > 0)
                {
                    Db.Insertable(addvalueList).ExecuteCommand();
                }
                if (editvalueList != null && editvalueList.Count > 0)
                {
                    Db.Updateable(editvalueList).UpdateColumns(it => new { it.BASEVALUE, it.MINVALUE, it.MAXVALUE, it.EDITTIME }).ExecuteCommand();
                }
                Db.CommitTran();
                return true;
            }
            catch (Exception)
            {
                Db.Ado.RollbackTran();
                return false;
            }
        }

        public bool DoDelete(string id)
        {
            return Db.Deleteable<HC_EXTEND_FIELD>().Where(it => it.ID == id).ExecuteCommand() > 0;
        }

        /// <summary>
        /// 判断列名是否重复
        /// </summary>
        /// <param name="filedName"></param>
        /// <returns></returns>
        public bool AddIsRepeated(string filedName)
        {
            //var exp = Expressionable.Create<HC_EXTEND_FIELD>();
            //exp.Or(it => it.NAME.Contains(filedStr));//拼接OR
            //exp.Or(it => it.TITLE.Contains(filedStr));//拼接OR
            return Db.Queryable<HC_EXTEND_FIELD>().Any(x => x.NAME == filedName);
        }


        /// <summary>
        /// 判断列名是否重复
        /// </summary>
        /// <param name="filedName"></param>
        /// <returns></returns>
        public bool EditIsRepeated(string id, string filedName)
        {
            //var exp = Expressionable.Create<HC_EXTEND_FIELD>();
            //exp.Or(it => it.NAME == filedStr);//拼接OR
            //exp.Or(it => it.TITLE == filedStr);//拼接OR
            return Db.Queryable<HC_EXTEND_FIELD>().Where(it => it.ID != id).Any(x => x.NAME == filedName);
        }

        public HC_EXTEND_FIELD GetById(string id)
        {
            return Db.Queryable<HC_EXTEND_FIELD>().Single(it => it.ID == id);
        }

        public string GetNameById(string id)
        {
            string sql = string.Format("SELECT NAME FROM HC_EXTEND_FIELD WHERE ID='{0}'", id);
            return Db.Ado.GetString(sql);
        }

        public HC_EXTEND_FIELD_VALUE GetFieldValueById(string fieldId)
        {
            var fieldModel = Db.Queryable<HC_EXTEND_FIELD>().Single(it => it.ID == fieldId);
            HC_EXTEND_FIELD_VALUE valueModel = new HC_EXTEND_FIELD_VALUE()
            {
                ID = fieldId,
                NAME = fieldModel.NAME,
                TITLE = fieldModel.TITLE,
                CONTROL_TYPE = fieldModel.CONTROL_TYPE,
                DEFAULT_VALUE = fieldModel.DEFAULT_VALUE,
                SORT = fieldModel.SORT,
                ENABLE = fieldModel.ENABLE,
                ReferenceValue = "",//new HC_FIELD_REFERENCE_VALUE_Repository().GetReferenceValue(fieldId),
                REMARK = Utils.ObjectToStr(fieldModel.REMARK),
            };
            return valueModel;
        }

        public EXTENDFIELD_REFERENCEVALUE GetEntityByID(string extendField)
        {
            var fieldModel = Db.Queryable<HC_EXTEND_FIELD>().Single(it => it.ID == extendField);
            if (fieldModel != null)
            {
                EXTENDFIELD_REFERENCEVALUE valueModel = new EXTENDFIELD_REFERENCEVALUE()
                {
                    ID = fieldModel.ID,
                    NAME = fieldModel.NAME,
                    TITLE = fieldModel.TITLE,
                    CONTROL_TYPE = fieldModel.CONTROL_TYPE,
                    IS_REQUIRED = fieldModel.IS_REQUIRED,
                    SORT = fieldModel.SORT,
                    ENABLE = fieldModel.ENABLE,
                    REMARK = Utils.ObjectToStr(fieldModel.REMARK),
                };
                valueModel.ReferenceValues = JsonHandler.SerializeObject(new HC_FIELD_REFERENCE_VALUE_Repository().GetReferenceValue(extendField));
                return valueModel;
            }
            else
            { return null; }
        }
    }
}
